home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter08
/
TranslatePanel.java
< prev
next >
Wrap
Text File
|
2000-06-26
|
2KB
|
84 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class TranslatePanel extends Shout3DPanel implements DeviceObserver{
Transform boxTrans;
float worldPosX;
int pixelStartX;
int pixelEndX;
public TranslatePanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
//get reference to the Transform node you need
boxTrans = (Transform) getNodeByName("mytrans");
//get the x position value from the transform
//and store it in the worldPosX variable
worldPosX = boxTrans.translation.getValue()[0];
//print out the value in worldPosX
//to confirm that starting x value is 0
System.out.println("Box's x position is " + worldPosX);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
return true;
case MouseInput.DRAG:
pixelEndX = mi.x;
//get pixel distance dragged
int dragDistance = pixelEndX - pixelStartX;
//convert pixel distance to meters
// at 100 pixels/1 meter
float deltaX = dragDistance/100f;
//add delta to current X position
//to get new position
worldPosX = worldPosX + deltaX;
//put the new X position
//in the transform node.
boxTrans.translation.set1Value(0, worldPosX);
//reset the starting pixel for next drag
pixelStartX = pixelEndX;
return true;
}//end of switch
return false;
}
} //end of class